package net.net23.sysdent.client.ui;

import java.util.Map;

import net.net23.sysdent.client.controller.AppController;
import net.net23.sysdent.client.ui.menu.MenuItem;
import net.net23.sysdent.client.ui.menu.MenuItemHandler;
import net.net23.sysdent.shared.MapServiceContants;

import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PushButton;
import com.google.gwt.user.client.ui.VerticalPanel;

public class T4LHeader extends Composite implements MenuItemHandler {

	private HorizontalPanel headerToolBar;

	private Label user;

	private PushButton settings;

	private PushButton exit;

	protected String logoutUrl;

	public T4LHeader() {

		VerticalPanel vp = new VerticalPanel();
		vp.setWidth("100%");

		headerToolBar = new HorizontalPanel();
		headerToolBar.setStyleName("t4l-header");
		headerToolBar
				.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);

		FlexTable headerRightToolBar = new FlexTable();
		// FlexCellFormatter flexCellFormatter = headerRightToolBar
		// .getFlexCellFormatter();

		int column = 0;
		
		user = new Label("");
		user.setStyleName("t4l-header-user");
		headerRightToolBar.setWidget(0, column++, user);

		Label separator1 = new Label("|");
		separator1.setStyleName("t4l-header-separator");
		headerRightToolBar.setWidget(0, column++, separator1);
		
		MenuItem prefs = new MenuItem("Opciones", "options");
		prefs.setMainClass("t4l-menu-item-black");
		prefs.addMenuItemHandler(this);
		headerRightToolBar.setWidget(0, column++, prefs);
		
		Label separator2 = new Label("|");
		separator2.setStyleName("t4l-header-separator");
		headerRightToolBar.setWidget(0, column++, separator2);

		MenuItem exit = new MenuItem("Salir", "exit");
		exit.setMainClass("t4l-menu-item-black");
		exit.addMenuItemHandler(this);
		headerRightToolBar.setWidget(0, column++, exit);

		headerToolBar.add(headerRightToolBar);

		vp.add(headerToolBar);
		vp.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LEFT);
		vp.add(new Image(AppController.images.logot4l()));

		initWidget(vp);
	}

	public static native void redirect(String url)/*-{
		$wnd.location = url;
	}-*/;

	public void setUserInfo(Map<Object, Object> result) {
		if (result != null) {
			String nickName = (String) result
					.get(MapServiceContants.GET_USER_INFO_NICK_NAME);
			if (nickName != null) {
				user.setText(nickName);
			}
			this.logoutUrl = (String) result
					.get(MapServiceContants.GET_LOGOUT_URL_URL);
		}
	}

	@Override
	public void itemSelected(MenuItem source) {
		String key = source.getKey();
		if ("exit".equals(key)) {
			if (T4LHeader.this.logoutUrl != null) {
				redirect(T4LHeader.this.logoutUrl);
			}
		}
	}
}
